package com.xmhmyh.generalframework.project.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmhmyh.generalframework.common.BaseController;
import com.xmhmyh.generalframework.common.constants.Constants;
import com.xmhmyh.generalframework.common.entity.DataGridRequest;
import com.xmhmyh.generalframework.common.entity.Response;
import com.xmhmyh.generalframework.common.enums.*;
import com.xmhmyh.generalframework.framework.aspectj.annotation.Log;

import com.xmhmyh.generalframework.framework.exceptions.ManageExceptions;
import com.xmhmyh.generalframework.project.sys.dto.ParamsDTO;
import com.xmhmyh.generalframework.project.sys.entity.Params;
import com.xmhmyh.generalframework.project.sys.entity.User;
import com.xmhmyh.generalframework.project.sys.enums.ParamsType;
import com.xmhmyh.generalframework.project.sys.service.IParamsService;
import com.xmhmyh.generalframework.project.sys.entity.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * 系统参数 Controller
 *
 * @author 于丁
 * @date 2020-08-09 11:05:29
 */
@Api(tags = "系统参数相关接口")
@RestController
@RequestMapping("/sys/params")
public class ParamsController extends BaseController {

    @Autowired
    private IParamsService paramsService;

    @Log(busType = LogBusType.EDITANDSAVE, source = LogSourceType.MANAGE, module = LogModule.PARAMS)
    @ApiOperation(value = "保存系统参数接口")
    @PreAuthorize("hasAuthority('sys:params:save')")
    @PostMapping("save")
    public Response saveHandler(@RequestBody ParamsDTO paramsDTO) {
        paramsService.saveHandler(getLoginUser().getUser(), paramsDTO);
        return Response.success();
    }


    @Log(busType = LogBusType.DELETE, source = LogSourceType.MANAGE, module = LogModule.PARAMS)
    @PreAuthorize("hasAuthority('sys:params:del')")
    @ApiOperation(value = "删除系统参数")
    @PostMapping("del")
    public Response delHandler(Long id) {
        User user = getLoginUser().getUser();
        Params params = paramsService.getById(id);
        if (params == null) {
            throw new ManageExceptions(ResponseCode.ENTITY_NOT_FIND);
        }
        if (params.getType() == ParamsType.OPT.getValue()) {
            if (!user.getAccountId().equals(Constants.SUPER_ACCOUNT_ID)) {
                throw new ManageExceptions(ResponseCode.OPT_PARAMS_NOT_REMOVE);
            }
        }
        if (params.getType() == ParamsType.OPT.getValue()) {
            paramsService.removeByCode(params.getCode());
        } else {
            paramsService.removeById(params.getId());
        }
        return Response.success();
    }

    @PreAuthorize("hasAuthority('sys:params:list')")
    @ApiOperation(value = "查找系统参数列表")
    @PostMapping("pagination")
    public Response paginationHandler(@RequestBody DataGridRequest params) {
        // 如果需要账户约束打开下面注解
        params = addAccountConstraints(params);
        params.setSortOrder(SortOrder.DESC.getLabel());
        params.setSortName("createTime");
        IPage<Params> paramsIPage = paramsService.paginate(params);
        return Response.success(paramsIPage);
    }


    @PreAuthorize("hasAuthority('sys:params:edit')")
    @ApiOperation(value = "获取系统参数的详细数据")
    @PostMapping("loadMore")
    public Response loadMoreHandler(@RequestParam Long id) {
        Params params = paramsService.getById(id);
        return Response.success(params);
    }
}
